package GUI;


import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Font;
import javax.swing.JComboBox;

import client.ClientConsole;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.io.IOException;

/**
 * show workers
 * @author Maor
 *
 */
public class GUI_ShowWorkers extends JPanel
{
	/**
	 * client Connected
	 */
	private ClientConsole clc;
	/** combo box for select worker id*/
	private JComboBox<String> comboBox;
	/** text field for worker id*/
	private JTextField textField;
	/** text field for first name*/
	private JTextField textField_1;
	/** text field for last name*/
	private JTextField textField_2;
	/** text field for email*/
	private JTextField textField_3;
	/** text field for job*/
	private JTextField textField_4;
	/** text field for user name*/
	private JTextField textField_5;
	private ArrayList<String[]> list;
	
	/**
	 * constructor that called from Manger menu, when the manager want to show workers
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param mm  Manger menu, for back
	 *
	 */
	public GUI_ShowWorkers(ClientConsole cc, final GUI_LogIn gl,final GUI_ManagerMenu mm) {
		setLayout(null);
		clc=cc;
		this.setSize(700, 600);
		JLabel lblWorkersDetails = new JLabel("Workers Details");
		lblWorkersDetails.setBounds(187, 11, 152, 37);
		lblWorkersDetails.setFont(new Font("Tahoma", Font.BOLD, 14));
		add(lblWorkersDetails);
		
		JLabel lblChooseWorker = new JLabel("Choose Worker:");
		lblChooseWorker.setBounds(39, 56, 100, 26);
		add(lblChooseWorker);
		
		try {
			String[] s={"getWorkers"};
			clc.client.sendToServer(s);
			while(clc.client.flag==0) System.out.print("");
			clc.client.flag=0;
			list=clc.client.details;
			comboBox = new JComboBox<String>();
			System.out.println(list.size());
			for(int i=0;i<list.size();i++)
			{
				String[] res=list.get(i);
				comboBox.addItem(res[0]);
			}
			comboBox.setBounds(168, 59, 115, 20);
			add(comboBox);
			comboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					String change=e.getItem().toString();
					String[] s={"getWorkerData",change};
					try {
						clc.client.sendToServer(s);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					while(clc.client.flag==0)
						System.out.print("");
					clc.client.flag=0;
					showResults();
				}
			});

		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		JButton btnLogout = new JButton("Log Out");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogout.setBounds(372, 330, 89, 23);
		add(btnLogout);
		
		JLabel lblReaderId = new JLabel("Worker Id");
		lblReaderId.setBounds(39, 114, 89, 14);
		add(lblReaderId);
		
		textField = new JTextField();
		textField.setBounds(159, 111, 140, 20);
		add(textField);
		textField.setColumns(10);
		textField.setEditable(false);
		
		JLabel lblUserName = new JLabel("First Name");
		lblUserName.setBounds(39, 139, 100, 14);
		add(lblUserName);
		
		textField_1 = new JTextField();
		textField_1.setBounds(159, 139, 140, 20);
		add(textField_1);
		textField_1.setColumns(10);
		textField_1.setEditable(false);
		
		JLabel lblPhone = new JLabel("Last Name");
		lblPhone.setBounds(39, 170, 100, 14);
		add(lblPhone);
		
		textField_2 = new JTextField();
		textField_2.setBounds(159, 167, 140, 20);
		add(textField_2);
		textField_2.setColumns(10);
		textField_2.setEditable(false);
		
		JLabel lblNewLabel = new JLabel("Email");
		lblNewLabel.setBounds(39, 201, 66, 14);
		add(lblNewLabel);
		
		textField_3 = new JTextField();
		textField_3.setBounds(159, 198, 140, 20);
		add(textField_3);
		textField_3.setColumns(10);
		textField_3.setEditable(false);
		
		textField_4 = new JTextField();
		textField_4.setBounds(159, 229, 140, 20);
		add(textField_4);
		textField_4.setColumns(10);
		textField_4.setEditable(false);
		
		JLabel lblLastName = new JLabel("Job");
		lblLastName.setBounds(39, 232, 66, 14);
		add(lblLastName);
		
		textField_5 = new JTextField();
		textField_5.setBounds(159, 254, 140, 20);
		add(textField_5);
		textField_5.setColumns(10);
		textField_5.setEditable(false);
		
		JLabel lblEmail = new JLabel("User Name");
		lblEmail.setBounds(39, 257, 100, 14);
		add(lblEmail);
		
	     JButton btnBack = new JButton("Back");
			btnBack.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					gl.setContentPane(mm);
				}
			});
			btnBack.setBounds(262, 330, 89, 23);
			add(btnBack);
			
			this.setVisible(true);
	}
	
	/**
	 * showin the worker the details in the texts field
	 */
	public void showResults()
	{
		ArrayList<String[]> result=clc.client.details;	
		textField.setText(result.get(0)[0]);
		textField_1.setText(result.get(0)[1]);
		textField_2.setText(result.get(0)[2]);
		textField_3.setText(result.get(0)[3]);
		textField_4.setText(result.get(0)[4]);
		textField_5.setText(result.get(0)[5]);
	}
}
